package com.hyny.framework.platform.system.config;


import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;


@Configuration
@EnableSwagger2WebMvc

public class Knife4jConfig {
    private final OpenApiExtensionResolver openApiExtensionResolver;

    @Value("${hyny.knife4j.version}")
    private String version = "";
    @Value("${hyny.knife4j.basePackageSys}")
    private String basePackageSys = "";
    @Value("${hyny.knife4j.basePackageBiz}")
    private String basePackageBiz = "";
    @Value("${hyny.jwt.tokenHerderKey}")
    private String tokenHerderKey;


    @Autowired
    public Knife4jConfig(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    @Bean
    public Docket dockerSystemBean() {

        //指定使用Swagger2规范
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(getParameterList())
                .apiInfo(new ApiInfoBuilder()
                        .title("系统管理")//名称
                        //描述字段支持Markdown语法
                        .description("系统管理【用户管理、角色管理、菜单管理】")//简介藐视
                        .termsOfServiceUrl("/")//服务条款
                        .contact(new Contact("惠油","/","/"))//作者信息
                        .version(version)//版本号
                        .build())
                //分组名称
                .groupName("系统管理")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage(basePackageSys))
                .paths(PathSelectors.any())
                .build()  .extensions(openApiExtensionResolver.buildExtensions("系统管理"));
        return docket;
    }

    @Bean
    public Docket dockerBizBean() {
        //指定使用Swagger2规范
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(getParameterList())
                .apiInfo(new ApiInfoBuilder()

                        .title("业务模块")//项目名称
                        //描述字段支持Markdown语法
                        .description("业务相关信息")//简介藐视
                        .termsOfServiceUrl("/")//服务条款
                        .contact(new Contact("惠油","/","/"))//作者信息
                        .version(version)//版本号
                        .build())
                //分组名称
                .groupName("业务模块")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage(basePackageBiz))
                .paths(PathSelectors.any())
                .build()  .extensions(openApiExtensionResolver.buildExtensions("业务模块"));
        return docket;
    }


    private List<Parameter> getParameterList() {
        ParameterBuilder clientIdTicket = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        clientIdTicket.name(tokenHerderKey).description("token令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build(); //设置false，表示clientId参数 非必填,可传可不传！
        pars.add(clientIdTicket.build());
        return pars;
    }

}
